package org.study.module.common;

import org.study.shell.ShellConstant;
import org.study.shell.ShellFile;
import org.study.shell.ShellUser;

import java.util.ArrayList;
import java.util.List;

public class ShellPre {

    /**
     * 前置准备：在每一条机器上创建用户和用户组，软件地址和安装地址
     *
     * @param group       用户组
     * @param user        用户
     * @param softPath    软件地址
     * @param installPath 安装地址
     */
    public static List<String> preInstallAddGroup(String group, String user, String password, String softPath, String installPath) {
        return preInstall(group, user, password, softPath, installPath);
    }

    /**
     * 前置准备，在每个机器上创建用户，用户组使用wheel
     *
     * @param user        用户
     * @param uploadPath  上传地址
     * @param installPath 安装地址
     */
    public static List<String> preInstallByWheel(String user, String password, String uploadPath, String installPath) {
        return preInstall(ShellConstant.WHEEL, user, password, uploadPath, installPath);
    }

    private static List<String> preInstall(String group, String user, String password, String uploadPath, String installPath) {
        List<String> commands = new ArrayList<String>();
        if (!ShellConstant.WHEEL.equals(group)) {
            commands.addAll(ShellUser.addGroup(group));
        }
        commands.addAll(ShellUser.deleteUser(user));
        commands.addAll(ShellUser.addUser(user, group, password));
        if (!ShellConstant.WHEEL.equals(group)) {
            commands.addAll(ShellUser.addWheel(user));
            commands.add(ShellUser.userAddSudo(user));
        }
        commands.addAll(ShellFile.createPathExist(installPath));
        commands.addAll(ShellFile.createPathExist(softwarePath(uploadPath)));
        commands.addAll(ShellFile.createPathExist(shellPath(uploadPath)));
        commands.addAll(ShellFile.createPathExist(configPath(uploadPath)));
        commands.add(ShellFile.chownPath(user, group, installPath));
        commands.add(ShellFile.chownPath(user, group, uploadPath));
        return commands;
    }
    public static String softwarePath(String uploadPath){
        return uploadPath + "/software";
    }
    public static String shellPath(String uploadPath){
        return uploadPath + "/shell";
    }
    public static String configPath(String uploadPath){
        return uploadPath + "/config";
    }
}
